package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
//@Transactional mongodb事务必须复制集方式
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);//防止游标
        strategyComment.setCreateTime(new Date());
        repository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {
        //查询的条件对象
        Query query = new Query();
        //拼接条件
        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(new Criteria().where("strategyId").is(qo.getStrategyId()));
        }
        long count = template.count(query, StrategyComment.class);//计算一共有多少条数据 query 为无条件 totalCount  集合MOngoDb
        if (count == 0) {
            return Page.empty();//空的页面对象
        }
        //分页加排序---->条件拼接
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //把条件放入查询对象中
        query.with(pageRequest);
        //查询当前结果集template.find
        List<StrategyComment> list = template.find(query, StrategyComment.class);
        //PageImpl等价于之前的PageResult
        return new PageImpl<>(list, pageRequest, count);//PageResult原生的  pageRequest当前页  多少条数
        //PageHadler  分页插件的
    }

    @Override
    public void commentThumb(String id, String cid) {
        StrategyComment strategyComment = this.get(cid);
        int thumbupnum = strategyComment.getThumbupnum();
        List<String> thumbuplist = strategyComment.getThumbuplist();
        if (thumbuplist.contains(id)) {
            thumbupnum = thumbupnum - 1;
            strategyComment.setThumbupnum(thumbupnum);
            thumbuplist.remove(id);
        } else {
            thumbupnum = thumbupnum + 1;
            strategyComment.setThumbupnum(thumbupnum);
            thumbuplist.add(id);
        }
        this.update(strategyComment);
    }
}